/*
 *  Copyright 2003 by Texas Instruments Incorporated.
 *  All rights reserved. Property of Texas Instruments Incorporated.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.
 *  
 */
#include <std.h>
#include <csl.h>
#include <csl_dat.h>
#include <csl_cache.h>
#include <chan.h>
#include <scom.h>
#include <utl.h>
#include <fvid.h>
#include <tsk.h>

#include <edc.h>
#include <vport.h>
#include <vportcap.h>
#include <tvp51xx.h>    

#include <evmdm642.h>
#include <evmdm642_capParamsSDTVDefault.h>

#include "appMain.h"
#include "appThreads.h"
#include "tskVideoInput.h"
#include "prepost.h"

VPORT_PortParams EVMDM642_vCapParamsPort 
  = EVMDM642_CAP_PARAMS_PORT_EMBEDDED_DEFAULT;

#if defined _NTSC

static VPORTCAP_Params EVMDM642_vCapParamsChan 
  = EVMDM642_CAP_PARAMS_CHAN_EMBEDDED_DEFAULT(NTSC720);

static TVP51XX_ConfParams EVMDM642_vCapParamsTVP5146 
  = EVMDM642_CAP_PARAMS_TVP51XX_EMBEDDED_DEFAULT(NTSC601, COMPOSITE, 0);

#elif defined _PAL

static VPORTCAP_Params EVMDM642_vCapParamsChan 
  = EVMDM642_CAP_PARAMS_CHAN_EMBEDDED_DEFAULT(PAL720);

static TVP51XX_ConfParams EVMDM642_vCapParamsTVP5146 
  = EVMDM642_CAP_PARAMS_TVP51XX_EMBEDDED_DEFAULT(PAL601, COMPOSITE, 0);
#endif


unsigned char YArray_temp1[FRM_WIDTH*FRM_HEIGHT];
unsigned char CbArray_temp1[(FRM_WIDTH*FRM_HEIGHT)>>1];
unsigned char CrArray_temp1[(FRM_WIDTH*FRM_HEIGHT)>>1];
unsigned char YArray_temp2[FRM_WIDTH*FRM_HEIGHT];
unsigned char CbArray_temp2[(FRM_WIDTH*FRM_HEIGHT)>>1];
unsigned char CrArray_temp2[(FRM_WIDTH*FRM_HEIGHT)>>1];

#pragma DATA_SECTION(scratch1,".internal_data1");
#pragma DATA_ALIGN(scratch1,8);
unsigned char scratch1[20*LINE_SZ];

ThrInput thrInput;
FVID_Handle  capChan;
FVID_Frame *capFrameBuf;


/*-----------------------------------------------------------*/
/* heap IDs defined in the BIOS configuration file           */
/*-----------------------------------------------------------*/
extern int extHeap;

void tskVideoInputInit()
{
    
    int status;    
    /******************************************************/
    /* allocate capture  frame buffers                    */
    /* in external heap memory                            */
    /******************************************************/
     EVMDM642_vCapParamsChan.segId = extHeap ;//EXTERNALHEAP;
     EVMDM642_vCapParamsTVP5146.hI2C = EVMDM642_I2C_hI2C;
     capChan = FVID_create("/VP0CAPTURE/A/0", 
        IOM_INPUT, &status, (Ptr)&EVMDM642_vCapParamsChan, NULL);

	 FVID_control(capChan, VPORT_CMD_EDC_BASE + EDC_CONFIG, 
      (Ptr)&EVMDM642_vCapParamsTVP5146);


}


/*-----------------------------------------------------------*/
/*Start the capturing                                        */
/*-----------------------------------------------------------*/
void tskVideoInputStart() 
{
	FVID_control(capChan, VPORT_CMD_START, NULL);
}


/*-----------------------------------------------------------*/
/* handle video capture, scaling and re-format               */
/*-----------------------------------------------------------*/
void tskVideoInput()
{
    int i=0,frame=0;
    SCOM_Handle fromInputtoProc,fromProctoInput;
    IMG in1,out1;
    SCRATCH scr;
    
    fromInputtoProc = SCOM_open("INTOPROC");
    fromProctoInput = SCOM_open("PROCTOIN");

	FVID_alloc(capChan, &capFrameBuf);

 	while(1)
 	{
	    frame++;
	    for(i = 0; i<NUM_SCOM_OBJS_IN; i++) 
		{

			in1.Y_data = (unsigned char *)capFrameBuf->frame.iFrm.y1;
     		in1.Cb_data = (unsigned char *)capFrameBuf->frame.iFrm.cb1;
			in1.Cr_data = (unsigned char *)capFrameBuf->frame.iFrm.cr1;
	 		scr.in_data = scratch1;		

	 		out1.Y_data = YArray_temp1;
	 		out1.Cb_data = CbArray_temp1;
	 		out1.Cr_data = CrArray_temp1;

	 		/*-----------------------------------------------------------*/
	 		/* Call YUV422to420v to do chroma resampling                 */
	 		/*-----------------------------------------------------------*/
	 		YUV422to420v(&in1,&out1,720,MAX_NUM_LINES_PER_FIELD*2,&scr);
	 		(thrInput.scomMsgTx).bufChannel = (void *)&out1;		

        	/*-----------------------------------------------------------*/
	   		/* Send message to process task with pointers to captured frame*/
	   		/*-----------------------------------------------------------*/
        	SCOM_putMsg(fromInputtoProc, &(thrInput.scomMsgTx));
			
			/*-----------------------------------------------------------*/
	   		/* Wait for the message from the process task                */
	   		/*-----------------------------------------------------------*/
        	SCOM_getMsg(fromProctoInput, SYS_FOREVER);
        	
        	/*-----------------------------------------------------------*/
        	/* Capture a new frame.                                      */
        	/*-----------------------------------------------------------*/
        	FVID_exchange(capChan, &capFrameBuf);
	 	}
	 }	    

}
